package com.link2room.aeriabots.controller.manager;

import com.link2room.aeriabots.dto.param.inside.WxQrcodeInsideParamDto;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.aeriabots.dto.param.api.WxReplyKeyApiParamDto;
import com.link2room.aeriabots.dto.result.api.WxReplyKeyMappingDto;
import com.link2room.aeriabots.dto.result.api.WxReplyQrCodeDto;
import com.link2room.aeriabots.facade.IQRCodeFacadeService;
import com.link2room.aeriabots.facade.IWXReplyFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.link2room.aeriabots.constant.PermissionConstant.*;

/**
 * Created by LN on 2016/12/8.
 */
@Api(tags = "微信公众号关键字回复相关")
@RestController
@RequestMapping("/manager/ReplyKeys")
public class WxReplyKeyController {

    @Resource
    private IWXReplyFacadeService wxReplyFacadeService;
    @Resource
    private IQRCodeFacadeService qrcodeFacadeService;

    @ApiOperation(value = "关键字回复列表", response = WxReplyKeyMappingDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "keyType", value = "关键字类型", required = true, paramType = "query"),
    })
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = replyKeys, name = "微信关键字回复列表")
    public BaseResultDto<WxReplyKeyMappingDto> getKeys(
            @RequestParam(name = "keyType")String keyType
    ) throws Exception {
        WxReplyKeyApiParamDto paramDto = new WxReplyKeyApiParamDto();
        paramDto.setKeyType(keyType);
        QueryResult res = wxReplyFacadeService.getReplyKeys(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setQueryResult(res);
        return resultDto;
    }

    /**
     * 查询可用二维码
     *
     * @return
     */
    @ApiOperation(value = "二维码列表", response = WxReplyQrCodeDto.class)
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/Qrcodes", method = RequestMethod.GET)
    @L2RPermissionResources(value = qrcodes, name = "查询可用二维码回复")
    public BaseResultDto<WxReplyQrCodeDto> getQrcodes(
    ) throws Exception {
        WxQrcodeInsideParamDto paramDto = new WxQrcodeInsideParamDto();
        QueryResult<WxReplyQrCodeDto> res = qrcodeFacadeService.getWxReplyQrCodes(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setQueryResult(res);
        return resultDto;
    }

    /**
     * 创建
     *
     * @param paramDto
     * @return
     */
    @ApiOperation(value = "创建关键字回复")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "param", dataTypeClass = WxReplyKeyApiParamDto.class)
    })
    @RequestMapping(method = RequestMethod.POST)
    @L2RPermissionResources(value = createKey, name = "创建可用二维码回复")
    public BaseResultDto<WxReplyKeyMappingDto> createKey(
            @RequestBody WxReplyKeyApiParamDto paramDto
    ) throws Exception {
        wxReplyFacadeService.createKey(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }

    /**
     * 修改
     */
    @ApiOperation(value = "关键字回复更新")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "param", dataTypeClass = WxReplyKeyApiParamDto.class)
    })
    @RequestMapping(method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateKey, name = "修改关键字")
    public BaseResultDto<WxReplyKeyMappingDto> updateKey(
            @RequestBody WxReplyKeyApiParamDto paramDto
    ) throws Exception {
        wxReplyFacadeService.updateKey(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }

    /**
     * 删除
     */
    @ApiOperation(value = "关键字回复删除")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "param", dataTypeClass = WxReplyKeyApiParamDto.class)
    })
    @RequestMapping(method = RequestMethod.DELETE)
    @L2RPermissionResources(value = deleteKey, name = "删除关键回复")
    public BaseResultDto<WxReplyKeyMappingDto> deleteKey(
            @RequestBody WxReplyKeyApiParamDto paramDto
    ) throws Exception {
        wxReplyFacadeService.deleteKey(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }
}
